package com.xjscrm.server.service.customer.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.google.common.collect.Lists;
import com.uzai.mobile.collect.api.MicaTopics;
import com.uzai.mobile.collect.api.dto.MicaTransDto;
import com.uzai.mobile.collect.api.dto.MicaTriggerDto;
import com.uzai.mobile.collect.api.dto.msg.MicaFriendAddMsg;
import com.uzai.mobile.collect.api.dto.msg.consertoserver.MicaCustomerAddTagMsg;
import com.uzai.mobile.collect.api.dto.trigger.MicaTriggerBatchMarkLabelMsg;
import com.uzai.mobile.collect.api.enums.MicaMsgType;
import com.uzai.mobile.collect.api.enums.MicaTriggerType;
import com.xjscrm.common.annotation.MerId;
import com.xjscrm.common.entity.WwCustomer;
import com.xjscrm.common.entity.WwCustomerTags;
import com.xjscrm.common.entity.WwTags;
import com.xjscrm.common.utils.DateUtil;
import com.xjscrm.common.utils.RandomUtil;
import com.xjscrm.common.utils.Tools;
import com.xjscrm.server.biz.mq.producer.SendMsgProducer;
import com.xjscrm.server.common.util.Functions;
import com.xjscrm.server.mapper.WwCustomerMapper;
import com.xjscrm.server.mapper.WwCustomerTagsMapper;
import com.xjscrm.server.mapper.WwTagsMapper;
import com.xjscrm.server.service.customer.WwCustomerTagsService;
import com.xjscrm.server.service.grouppull.GroupPullConfigService;
import com.xjscrm.server.vo.device.Device;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class CustomerTagsServiceImpl implements WwCustomerTagsService {
    @Autowired
    private WwCustomerTagsMapper wwCustomerTagsMapper;
    @Autowired
    private WwCustomerMapper wwCustomerMapper;
    @Autowired
    private SendMsgProducer sendMsgProducer;
    @Autowired
    private WwTagsMapper wwTagsMapper;
    @Resource
    private GroupPullConfigService groupPullConfigService;

    @Override
    public void add() {
        for (int i = 0; i < 5000; i++) {
            try {
                MicaFriendAddMsg micaFriendAddMsg = new MicaFriendAddMsg();
                micaFriendAddMsg.setWxid("youzai" + RandomUtil.getSmsCode(8));
                micaFriendAddMsg.setNickName("昵称=" + RandomUtil.getSmsCode(4));
                micaFriendAddMsg.setHeadImg("http://wx.qlogo.cn/mmhead/QtslCPzwA8Sq55sNK9N61ibwma6ic1vhFsndYZAC45chibuhrzZBxy21TvTydRu7WD2NLAd8iaqsDkc/0");
                micaFriendAddMsg.setScene(16);
                micaFriendAddMsg.setGender(1);
                micaFriendAddMsg.setContent("测试内容" + RandomUtil.getSmsCode(4));

                MicaTransDto micaTransDto = MicaTransDto.newInstance(micaFriendAddMsg, MicaMsgType.FRIEND_ADD, (ii) -> {
                });
                micaTransDto.setMerId(1547482020152213506L);
                micaTransDto.setDeviceId("1688857304636126");
                micaTransDto.setDeviceType(9);
                sendMsgProducer.pushMsg(micaTransDto, MicaTopics.WW_CONTACT_TOPIC);
            }catch (Exception e){
                log.error(e.getMessage(), e);
            }
        }
    }

    /**
     *  给会员打标签
     * @author jitwxs
     * @date 2024/3/19 17:42
     * @param merId
     * @return null
     */
    public void addWwCustomerTag(@MerId Long merId, String wxid, Long tagId, Device device) {

        int now = DateUtil.getTime();

        if (StringUtils.isBlank(wxid)) {
            return;
        }

        if (device == null) {
            return;
        }

        if (tagId == null) {
            return;
        }

        if (merId == null) {
            return;
        }

        WwTags wwTags = wwTagsMapper.queryById(tagId);
        if(wwTags == null){
            return;
        }

        WwCustomer wwCustomer = wwCustomerMapper.queryByDeviceUniqueIdAndWxid(device.getDeviceUniqueId(), wxid);
        if (wwCustomer == null) {//未查询到该会员，直接跳出
            return;
        }

        //判断该会员已经打了此标签
        WwCustomerTags wwCustomerTags = wwCustomerTagsMapper.queryByCustomerIdAndTagId(wwCustomer.getId(), tagId);
        if (wwCustomerTags != null) {
            //查询会员是否已经有此标签了，有此标签则不需要打
            return;
        }

        //插入会员标签
        wwCustomerTags = new WwCustomerTags();
        wwCustomerTags.setId(IdWorker.getId());
        wwCustomerTags.setMerId(device.getMerId());
        wwCustomerTags.setTagId(tagId);
        wwCustomerTags.setTagIdStr(wwTags.getTagId());
        wwCustomerTags.setCustomerId(wwCustomer.getId());
        wwCustomerTags.setCreateTime(now);
        wwCustomerTags.setUpdateTime(now);
        wwCustomerTagsMapper.insert(wwCustomerTags);
        if (Tools.getInteger(wwTags.getType()).intValue() == 1) {
            //根据标签拉群
            groupPullConfigService.groupPushByEnterpriseTag(device, wwCustomer, wwCustomerTags.getTagId());
        }
        //发送打标签指令
        MicaTriggerBatchMarkLabelMsg micaTriggerBatchMarkLabelMsg = new MicaTriggerBatchMarkLabelMsg();
        micaTriggerBatchMarkLabelMsg.setLabelId(wwTags.getTagId());
        micaTriggerBatchMarkLabelMsg.setIsCorp(Tools.getInteger(wwTags.getType()).intValue() == 1 ? true : false); //是否是企业标签
        micaTriggerBatchMarkLabelMsg.setAddWxIds(Lists.newArrayList(wwCustomer.getWxid()));
        MicaTriggerDto micaTriggerDto = MicaTriggerDto.newInstance(micaTriggerBatchMarkLabelMsg, MicaTriggerType.TRIGGER_BATCH_MARK_LABEL, Functions.fillTriggerMsg(device));
        sendMsgProducer.pushMsg(micaTriggerDto, MicaTopics.WW_TRIGGER_TOPIC); //存放瞬时消息
    }

    /**
     *  给会员打标签
     * @author jitwxs
     * @date 2024/3/19 17:42
     * @param merId
     * @return null
     */
    public void addWwCustomerTag(@MerId Long merId, WwCustomer wwCustomer, Long tagId, Device device) {

        int now = DateUtil.getTime();

        if (wwCustomer == null) {
            return;
        }

        if (device == null) {
            return;
        }

        if (tagId == null) {
            return;
        }

        if (merId == null) {
            return;
        }

        WwTags wwTags = wwTagsMapper.queryById(tagId);
        if(wwTags == null){
            return;
        }

        //判断该会员已经打了此标签
        WwCustomerTags wwCustomerTags = wwCustomerTagsMapper.queryByCustomerIdAndTagId(wwCustomer.getId(), tagId);
        if (wwCustomerTags != null) {
            //查询会员是否已经有此标签了，有此标签则不需要打
            return;
        }

        //插入会员标签
        wwCustomerTags = new WwCustomerTags();
        wwCustomerTags.setId(IdWorker.getId());
        wwCustomerTags.setMerId(device.getMerId());
        wwCustomerTags.setTagId(tagId);
        wwCustomerTags.setTagIdStr(wwTags.getTagId());
        wwCustomerTags.setCustomerId(wwCustomer.getId());
        wwCustomerTags.setCreateTime(now);
        wwCustomerTags.setUpdateTime(now);
        wwCustomerTagsMapper.insert(wwCustomerTags);
        if (Tools.getInteger(wwTags.getType()).intValue() == 1) {
            groupPullConfigService.groupPushByEnterpriseTag(device, wwCustomer, wwCustomerTags.getTagId());
        }

        //发送打标签指令
        MicaTriggerBatchMarkLabelMsg micaTriggerBatchMarkLabelMsg = new MicaTriggerBatchMarkLabelMsg();
        micaTriggerBatchMarkLabelMsg.setLabelId(wwTags.getTagId());
        micaTriggerBatchMarkLabelMsg.setIsCorp(Tools.getInteger(wwTags.getType()).intValue() == 1 ? true : false); //是否是企业标签
        micaTriggerBatchMarkLabelMsg.setAddWxIds(Lists.newArrayList(wwCustomer.getWxid()));
        MicaTriggerDto micaTriggerDto = MicaTriggerDto.newInstance(micaTriggerBatchMarkLabelMsg, MicaTriggerType.TRIGGER_BATCH_MARK_LABEL, Functions.fillTriggerMsg(device));
        sendMsgProducer.pushMsg(micaTriggerDto, MicaTopics.WW_TRIGGER_TOPIC); //存放瞬时消息
    }

    @Override
    public List<Long> getTagIds(@MerId Long merId, Long customerId) {
        List<WwCustomerTags> wwCustomerTagsList = wwCustomerTagsMapper.queryByCustomerId(customerId);
        List<Long> wwCustomerTagedList = new ArrayList<>();
        if (wwCustomerTagsList != null && wwCustomerTagsList.size() > 0) {
            for (WwCustomerTags wwCustomerTags : wwCustomerTagsList) {
                wwCustomerTagedList.add(wwCustomerTags.getTagId());
            }
        }
        return wwCustomerTagedList;
    }

}
